package com.vdg.fps;

import java.util.ArrayList;

import android.R.menu;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
import android.widget.TabWidget;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import com.vdg.R;
import com.vdg.ui.fragment.AccountsFragment;

public class MainActivity extends FragmentActivity {
	private static final int PICKER_ACTION = 111;
	TabHost mTabHost;
	ViewPager mViewPager;
	TabsAdapter mTabsAdapter;
	private static final String TAG = "MainActivity";
	private SlidingMenu mMenu;
	private LinearLayout mBtnExit;
	private RelativeLayout mBtnAbout;
	private RelativeLayout mBtnSetting;
	private RelativeLayout mBtnRateForUs;
	private RelativeLayout mBtnProVersion;
	private RelativeLayout mBtnHelps;
	private ImageButton mBtnMenu;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_tabs_pager);

		mTabHost = (TabHost) findViewById(android.R.id.tabhost);
		initSlideMenu();
		mTabHost.setup();

		mViewPager = (ViewPager) findViewById(R.id.pager);

		mTabsAdapter = new TabsAdapter(this, mTabHost, mViewPager, mMenu);
		mTabsAdapter
				.addTab(mTabHost.newTabSpec(
						getResources().getString(R.string.Accounts))
						.setIndicator(
								prepareTabView(
										this,
										getResources().getString(
												R.string.Accounts), 0,
										R.color.white)),
						AccountsFragment.class, null);
		mTabsAdapter
				.addTab(mTabHost.newTabSpec(
						getResources().getString(R.string.Accounts))
						.setIndicator(
								prepareTabView(
										this,"Chedules", 0,
										R.color.white)),
						AccountsFragment.class, null);
		if (savedInstanceState != null) {
			mTabHost.setCurrentTabByTag(savedInstanceState.getString("tab"));
		}
	}

	private static View prepareTabView(Context context, String title,
			int drawable, int color) {
		View view = LayoutInflater.from(context).inflate(
				R.layout.tab_indicator, null);
		TextView txvTitle = (TextView) view.findViewById(R.id.title);
		ImageView imvIcon = (ImageView) view.findViewById(R.id.icon);
		imvIcon.setImageResource(drawable);
		txvTitle.setText(title);
		return view;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putString("tab", mTabHost.getCurrentTabTag());
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu) {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// getMenuInflater().inflate(R.menu.activity_main, menu);
	// return true;
	// }

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	public static class TabsAdapter extends FragmentPagerAdapter implements
			TabHost.OnTabChangeListener, ViewPager.OnPageChangeListener {
		private final Context mContext;
		private final TabHost mTabHost;
		private final ViewPager mViewPager;
		private ArrayList<TabSpec> mListTabSpec;
		private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();
		private SlidingMenu mMenu;

		static final class TabInfo {
			private final String tag;
			private final Class<?> clss;
			private final Bundle args;

			TabInfo(String _tag, Class<?> _class, Bundle _args) {
				tag = _tag;
				clss = _class;
				args = _args;
			}
		}

		static class DummyTabFactory implements TabHost.TabContentFactory {
			private final Context mContext;

			public DummyTabFactory(Context context) {
				mContext = context;
			}

			@Override
			public View createTabContent(String tag) {
				View v = new View(mContext);
				v.setMinimumWidth(0);
				v.setMinimumHeight(0);
				return v;
			}
		}

		public TabsAdapter(FragmentActivity activity, TabHost tabHost,
				ViewPager pager, SlidingMenu menu) {
			super(activity.getSupportFragmentManager());
			mContext = activity;
			mTabHost = tabHost;
			mViewPager = pager;
			mMenu = menu;
			mTabHost.setOnTabChangedListener(this);
			mViewPager.setAdapter(this);
			mViewPager.setOnPageChangeListener(this);
			mListTabSpec = new ArrayList<TabHost.TabSpec>();
		}

		public void addTab(TabHost.TabSpec tabSpec, Class<?> clss, Bundle args) {
			tabSpec.setContent(new DummyTabFactory(mContext));
			String tag = tabSpec.getTag();
			mListTabSpec.add(tabSpec);
			TabInfo info = new TabInfo(tag, clss, args);
			mTabs.add(info);
			mTabHost.addTab(tabSpec);
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return mTabs.size();
		}

		@Override
		public Fragment getItem(int position) {
			TabInfo info = mTabs.get(position);
			return Fragment.instantiate(mContext, info.clss.getName(),
					info.args);
		}

		@Override
		public void onTabChanged(String tabId) {
			int position = mTabHost.getCurrentTab();
			mViewPager.setCurrentItem(position);
			if (tabId.equals(mContext.getResources().getString(
					R.string.Accounts))) {
				mMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
			} else {
				mMenu.setTouchModeAbove(SlidingMenu.LEFT);
			}
			Log.v("debug", "" + position + "       " + mListTabSpec.size());
		}

		@Override
		public void onPageScrolled(int position, float positionOffset,
				int positionOffsetPixels) {
		}

		@Override
		public void onPageSelected(int position) {
			TabWidget widget = mTabHost.getTabWidget();
			int oldFocusability = widget.getDescendantFocusability();
			widget.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
			mTabHost.setCurrentTab(position);
			widget.setDescendantFocusability(oldFocusability);
		}

		@Override
		public void onPageScrollStateChanged(int state) {
		}
	}

	private void initSlideMenu() {
		// mMenu=getSlidingMenu();
		mMenu = new SlidingMenu(this);
		mMenu.setMode(SlidingMenu.LEFT);
		mMenu.setTouchModeAbove(SlidingMenu.LEFT);
		mMenu.setShadowWidthRes(R.dimen.shadow_width);
		mMenu.setShadowDrawable(R.drawable.shadow);
		mMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		mMenu.setFadeDegree(0.35f);
		mMenu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
		mMenu.setMenu(R.layout.menu_frame);
		// mMenu.setSecondaryMenu(R.layout.menu_dialog_layout);
		// mMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		// mMenu.sho
		// setContentView(R.layout.menu_frame);
		// getSupportFragmentManager().beginTransaction()
		// .replace(R.id.menu_frame, new SampleListFragment()).commit();

		// getSlidingMenu().setSecondaryMenu(R.layout.menu_frame_two);
		// getSlidingMenu().setSecondaryShadowDrawable(R.drawable.shadowright);
		// getSupportFragmentManager().beginTransaction()
		// .replace(R.id.menu_frame_two, new SampleListFragment())
		// .commit();

		mBtnExit = (LinearLayout) mMenu.findViewById(R.id.btn_exit);
		mBtnExit.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		mBtnRateForUs = (RelativeLayout) mMenu
				.findViewById(R.id.btn_rate_for_us);
		mBtnRateForUs.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
		//
		mBtnAbout = (RelativeLayout) mMenu.findViewById(R.id.btn_about_us);
		mBtnAbout.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
		mBtnSetting = (RelativeLayout) mMenu.findViewById(R.id.btn_setting);
		mBtnSetting.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this,
						SettingActivity.class);
				startActivity(intent);
			}
		});
		mBtnProVersion = (RelativeLayout) mMenu
				.findViewById(R.id.btn_pro_version);
		mBtnProVersion.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
		//
		mBtnHelps = (RelativeLayout) mMenu.findViewById(R.id.btn_help);
		mBtnHelps.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			}
		});
		mBtnMenu = (ImageButton) findViewById(R.id.btn_menu);
		mBtnMenu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showMenu();
			}
		});
	}

	// @Override
	// public void onCreateContextMenu(ContextMenu menu, View v,
	// ContextMenuInfo menuInfo) {
	// menu.setHeaderTitle("aaaaaaaaaa");
	// menu.add(Menu.NONE, v.getId(), 1, "bbbbbbbbb1");
	// menu.add(Menu.NONE, v.getId(), 1, "bbbbbbbbb1");
	// menu.add(Menu.NONE, v.getId(), 1, "bbbbbbbbb1");
	// menu.add(Menu.NONE, v.getId(), 1, "bbbbbbbbb1");
	// menu.add(Menu.NONE, 2, 2, "bbbbbbbbb");
	// menu.add(Menu.NONE, 2, 2, "bbbbbbbbb");
	// menu.add(Menu.NONE, 2, 2, "bbbbbbbbb");
	// menu.add(Menu.NONE, v.getId(), 1, "bbbbbbbbb1");
	// menu.add(Menu.NONE, v.getId(), 1, "bbbbbbbbb1");
	// menu.add(Menu.NONE, v.getId(), 1, "bbbbbbbbb1");
	// menu.add(Menu.NONE, v.getId(), 1, "bbbbbbbbb1");
	// menu.add(Menu.NONE, 2, 2, "bbbbbbbbb");
	// menu.add(Menu.NONE, 2, 2, "bbbbbbbbb");
	// menu.add(Menu.NONE, 2, 2, "bbbbbbbbb");
	// }

	public void showMenu() {
		mMenu.showMenu(true);
	}
}
